package com.huika.dealontop.control.timelimit.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.support.domain.TimeProductBean;
import com.huika.dealontop.utils.DistanceUtils;
import com.huika.dealontop.utils.GMapUtil;
import com.huika.dealontop.utils.ImageLoaderHelper;
import com.huika.huikalib.utils.MoneyShowTool;
import com.huika.huikalib.utils.StringUtils;

import java.text.DecimalFormat;

/**
 * 分类附近子节点适配器
 */
public class TimeGrouponAdapter extends BaseAda<TimeProductBean> {
    private Context context;

    public TimeGrouponAdapter(Context context) {
        super(context);
        this.context = context;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh = null;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_project_detail, viewGroup, false);
            vh = new ViewHolder(view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        TimeProductBean nearbyLocallistbean = getItem(i);
        if (StringUtils.isEmpty(nearbyLocallistbean.getProductPicUrl())) {
            vh.iv_picture.setImageResource(R.mipmap.morenpic);
        } else {
            ImageLoaderHelper.displayImage(nearbyLocallistbean.getProductPicUrl(), vh.iv_picture);
        }
        vh.tv_name.setText(nearbyLocallistbean.getMerchantName());
        vh.tv_price.setText(MoneyShowTool.nolastDF(nearbyLocallistbean.getSalesPrice()) + "");
        vh.tv_zhekounum.setText(Math.round(nearbyLocallistbean.getDiscountRate()) + "%");
        if (new GMapUtil().getCurrentLat().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLat())
                || new
                GMapUtil().getCurrentLng().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLng())||new GMapUtil().getCurrentLng().equals("0.0")||new GMapUtil().getCurrentLat().equals("0.0")) {
            vh.tv_juli.setText(R.string.common_unknown);
        } else {
            try {
                if (nearbyLocallistbean.getDistance() > 0) {
                    vh.tv_juli.setText(DistanceUtils.formatMeter(nearbyLocallistbean.getDistance()));
                } else {
                    vh.tv_juli.setText(R.string.common_unknown);
                }
            } catch (Throwable t) {
                vh.tv_juli.setText(R.string.common_unknown);
            }
        }
        /*vh.tv_juli.setText(nearbyLocallistbean.getDistance() + "");*/
        if (Math.round(nearbyLocallistbean.getScoreAvg()) == 0) {
            vh.rb_pingfen.setRating(5f);
            vh.tv_pingfennum.setText("5.0");
        } else {
            vh.rb_pingfen.setRating(Math.round(nearbyLocallistbean.getScoreAvg()));
            vh.tv_pingfennum.setText(Math.round(nearbyLocallistbean.getScoreAvg()) + ".0");
        }

        return view;
    }

    private int formatPrice(float price) {
        try {
            DecimalFormat df = new DecimalFormat("0.0");
            return Integer.parseInt(df.format(price));
        } catch (Throwable t) {
            return 0;
        }
    }

    class ViewHolder {
        public ImageView iv_picture;
        public TextView tv_name, tv_price, tv_zhekounum, tv_juli, tv_pingfennum;
        private RatingBar rb_pingfen;

        public ViewHolder(View view) {
            iv_picture = (ImageView) view.findViewById(R.id.project_iv_icon);
            tv_name = (TextView) view.findViewById(R.id.project_tv_name);
            tv_price = (TextView) view.findViewById(R.id.project_tv_price);
            tv_zhekounum = (TextView) view.findViewById(R.id.project_tv_zhekounum);
            tv_juli = (TextView) view.findViewById(R.id.project_tv_juli);
            tv_pingfennum = (TextView) view.findViewById(R.id.project_tv_pingfennum);
            rb_pingfen = (RatingBar) view.findViewById(R.id.project_rb_pingfen);
        }
    }
}
